//
//  SRSettingTargetView.m
//  SportRing
//
//  Created by Apple on 14/10/24.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRSettingTargetView.h"
#import "SRViewSetting.h"

@interface SRSettingTargetView ()

@property (nonatomic, weak) IBOutlet UIButton * doneBtn;
@property (nonatomic, weak) IBOutlet UILabel * distanceLabel;
@property (nonatomic, weak) IBOutlet UILabel * caroliLabel;
@property (nonatomic, weak) IBOutlet LGTextField * targetTF;

@property (nonatomic , strong) UILabel *buLabel;

@end

@implementation SRSettingTargetView

- (void)awakeFromNib
{
    [super awakeFromNib];
    _targetTF.keyboardType = UIKeyboardTypeNumberPad;
    _targetTF.text = @"3000";
//    _targetTF.backgroundColor = [UIColor redColor];
    
    _targetTF.center = CGPointMake(CGRectGetMidX(self.bounds) - 5, CGRectGetMidY(_targetTF.superview.bounds));
    _buLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _buLabel.text = @"步";
    _buLabel.textColor = [UIColor lightGrayColor];
    _buLabel.font = [UIFont systemFontOfSize:13];
    [_targetTF.superview addSubview:_buLabel];
    _targetTF.superview.tag = 222;
    [self textChangeAction:nil];
    [self.targetTF addTarget:self action:@selector(textChangeAction:) forControlEvents:UIControlEventEditingChanged];
    
    RAC(self.doneBtn, enabled) = [RACSignal combineLatest:@[self.targetTF.rac_textSignal]
                                                   reduce:^(NSString * target){
                                                       return @(target.length > 0);
                                                   }];
    
    @weakify(self);
    
    [[self.doneBtn rac_signalForControlEvents:UIControlEventTouchUpInside]
     subscribeNext:^(id x) {
         [self_weak_.delegate didSelectTarget:[self_weak_.targetTF.text floatValue]];
         [self_weak_.delegate didTapDoneBtn];
     }];
    
    [[self.targetTF rac_textSignal] subscribeNext:^(id x) {
        
    }];
}

- (void)calValues
{
    float distance = [SRHelper distanceMeterWithHeightMeter:[SRViewSetting sharedSetting].height/100.0 steps:[self.targetTF.text floatValue]];
    
    int kalorie = [SRHelper caloriesWithHeightMeter:(float)[SRViewSetting sharedSetting].height/100.0 weight:[SRViewSetting sharedSetting].weight steps:[self.targetTF.text floatValue]];
    
    self.distanceLabel.text = [NSString stringWithFormat:@"%.1f千米",distance];
    self.caroliLabel.text = [NSString stringWithFormat:@"%d千卡",kalorie];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if (touch.view.tag == 222) {
        [_targetTF becomeFirstResponder];
    }else
    {
        [self endEditing:YES];
    }
}

- (void)textChangeAction:(id)sender
{
//    if (_targetTF.text.length == 0) {
//        _targetTF.text = @"3000";
//        [self textChangeAction:nil];
//    }
    [self calValues];
    [_targetTF sizeToFit];
    CGSize size = [self sizeWithString:_targetTF.text font:_targetTF.font];
    _targetTF.frame = CGRectMake(0, 0, size.width, _targetTF.frame.size.height);
    NSLog(@"%@",NSStringFromCGRect(_targetTF.frame));
    _targetTF.center = CGPointMake(CGRectGetMidX(self.bounds) - 5, CGRectGetMidY(_targetTF.superview.bounds));
    _buLabel.frame = CGRectMake(CGRectGetMaxX(_targetTF.frame), 18, 13, 15);
}

- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font
{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(1000, 1000)//显示的最大容量
                                       options: NSStringDrawingUsesLineFragmentOrigin //描述字符串的附加参数
                                    attributes:@{NSFontAttributeName: font}//描述字符串的参数
                                       context:nil];//上下文
    //返回值
    return rect.size;
}

@end
